//
//  ChannelViewController.m
//  FNer
//
//  Created by dllo on 15/11/4.
//  Copyright © 2015年 刘志强. All rights reserved.
//

#import "ChannelViewController.h"
#import "MoreViewController.h"
#import "myScrollView.h"
#import "CommunityCollectionViewCell.h"
#import "MyCollectionReusableView.h"
#import "Channels.h"
#import "Themes.h"
#import "Lunbo.h"
#import "WheelImageViewController.h"
#import "CommunityDetailViewController.h"

@interface ChannelViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,myCollectionReusableViewDelegate,pushDetailVC2>
@property (nonatomic, strong)BaseCollectionView *shequCV;
@property(nonatomic,retain)MyCollectionReusableView *areusableV;//头部区域
@property(nonatomic,retain)NSMutableArray *anewThemesArray;//装小标题的数组
@property (nonatomic, strong)myScrollView *lunboSV;//轮播图
@property(nonatomic,retain)NSMutableDictionary *chanellDic;//装重排序的chanell字典
@property(nonatomic,assign)BOOL isUpLoading;//记录下拉上拉刷新
@property(nonatomic,strong)NSMutableArray *lunboArray;
@property(nonatomic,retain)CommunityCollectionViewCell *shequCell;//全部频道页面

@property(nonatomic,retain)WheelImageViewController *wheelVC;//轮播图详情VC
//
@property(nonatomic,retain)CommunityDetailViewController *communityDetailVC;
//第零区头部内容属性
@property(nonatomic,retain)UIView *tuijianView;
@property(nonatomic,retain)UILabel *tuijianLabel;
@property(nonatomic,retain)UIImageView *tuijianImageView;
@property(nonatomic,retain)UIView *smallTilteView;
@property(nonatomic,retain)UIImageView *atuijianImageView;
@property(nonatomic,retain)UILabel *atuijianLabel;

@property(nonatomic,retain)NSMutableArray *myarray;

@end

@implementation ChannelViewController






- (void)viewDidLoad {
    [super viewDidLoad];
       // Do any additional setup after loading the view.


    
    self.wheelVC = [[WheelImageViewController alloc] init];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(355 * XWIDTH, 100 * XHIGHT);
    flowLayout.minimumLineSpacing = 1;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 10 * XWIDTH, 10 * XHIGHT, 10 * XWIDTH);
    
    self.shequCV = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0, 0, 375 * XWIDTH, 667 * XHIGHT - 64 - 49) collectionViewLayout:flowLayout];
    [self.view addSubview:self.shequCV];
    self.shequCV.delegate = self;
    self.shequCV.dataSource = self;
    [self.shequCV registerClass:[CommunityCollectionViewCell class] forCellWithReuseIdentifier:@"sehqucell"];

    self.lunboSV = [[myScrollView alloc]initWithFrame:CGRectMake(0, 0, 375 *XWIDTH, 240 *XHIGHT)];
    self.lunboSV.my = self;
    
    
    [self addHeaderRefresh];

    
}



//下拉刷新
-(void)addHeaderRefresh
{
   
   self.shequCV.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
       self.isUpLoading = NO;
       [self getShequData];
       [self getLunboData];

       
   }];
    [self.shequCV.header beginRefreshing];
}



//获得社区数据
-(void)getShequData
{
    NSString *url_string = [NSString stringWithFormat:@"http://api.imzhitu.com/operations/channel_queryHot"];
    
    NSDictionary *body = @{@"start":@"1",@"limit":@"20"};
    
    [AFNTools getAFN:url_string andpara:body andBlock:^(id result) {
        self.anewThemesArray = [NSMutableArray array];
        NSMutableDictionary *bigDic = result;
        
        NSMutableArray *themesArray = [bigDic objectForKey:@"themes"];
        for (NSMutableDictionary *dic in themesArray) {
            Themes *athemes = [[Themes alloc] init];
            [athemes setValuesForKeysWithDictionary:dic];
            [self.anewThemesArray addObject:athemes];
        }
        
        NSMutableArray *channelsArray = [bigDic objectForKey:@"channels"];
        self.chanellDic = [NSMutableDictionary dictionary];
        for (NSMutableDictionary *dic in channelsArray) {
            Channels *achannel = [[Channels alloc] init];
            [achannel setValuesForKeysWithDictionary:dic];
            for (int i = 0; i < self.anewThemesArray.count; i++) {
                Themes *atheme = [[Themes alloc] init];
                atheme = [self.anewThemesArray objectAtIndex:i];
                if (achannel.themeId == atheme.id) {
                    if ([self.chanellDic objectForKey:[NSString stringWithFormat:@"%ld",achannel.themeId]]) {
                        NSMutableArray *array =[NSMutableArray array];
                        array = [self.chanellDic objectForKey:[NSString stringWithFormat:@"%ld",achannel.themeId]];
                        [array addObject:achannel];
                        [self.chanellDic setObject:array forKey:[NSString stringWithFormat:@"%ld",achannel.themeId]];
                    }else{
                        NSMutableArray *array = [NSMutableArray array];
                        [array addObject:achannel];
                        [self.chanellDic setObject:array forKey:[NSString stringWithFormat:@"%ld",achannel.themeId]];
                    }
                }
            }
        }

        [self.shequCV.header endRefreshing];
        [self.shequCV reloadData];
        
    } andfailBlock:^(id fail) {
  
        
            NSLog(@"没有缓存");
     
         [self.shequCV.header endRefreshing];
    //    [self.shequCV reloadData];

    }];
    
    

}
//获得轮播图数据
-(void)getLunboData
{
    NSString *url_string = [NSString stringWithFormat:@"http://api.imzhitu.com/operations/msg_queryBulletin"];
    [AFNTools getAFN:url_string andpara:nil andBlock:^(id result) {
        self.lunboArray = [NSMutableArray array];
        NSMutableDictionary *bigDic = result;
        
     //   [[DataHandle shareDataHandle] lunboHuanCun:bigDic];
        
        NSMutableArray *array = [bigDic objectForKey:@"msg"];
        for (NSMutableDictionary *dic in array) {
            Lunbo *alunbo = [[Lunbo alloc] init];
            [alunbo setValuesForKeysWithDictionary:dic];
            [self.lunboArray addObject:alunbo];
        }
        self.lunboSV.myArray = self.lunboArray;
     //   [self getImagetoLunbo];
        [self.shequCV reloadData];

        
    } andfailBlock:^(id fail) {
        
   //     [self getImagetoLunbo];
   //     [self.shequCV reloadData];
        

    }];
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return CGSizeMake(375 * XWIDTH, 240 * XHIGHT);
    }
    else
    {
        return CGSizeMake(375 * XWIDTH, 40 * XHIGHT);
    }
}
//头部区域内容
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    [self.shequCV registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"one"];
    [self.shequCV registerClass:[MyCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"other"];
    
    if (indexPath.section == 0) {
         UICollectionReusableView *crv = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"one"forIndexPath:indexPath];
        [crv addSubview:self.lunboSV];
        return crv;
    }
    else
    {
        self.areusableV = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"other" forIndexPath:indexPath];
        if (self.areusableV == nil) {
            self.areusableV = [[MyCollectionReusableView alloc] initWithFrame:self.areusableV.bounds];
        }
        
        self.areusableV.atheme = [self.anewThemesArray objectAtIndex:indexPath.section];
        self.areusableV.myAgent = self;
        self.areusableV.mySection = indexPath.section;
        self.areusableV.setion = indexPath.section;
        return self.areusableV;
    }
    

}
//创建第零区头部内容
-(void)createZeroSectionHeadView
{
    self.tuijianView = [[UIView alloc] initWithFrame:CGRectMake(10 * XWIDTH, 210 * XHIGHT, 355 * XWIDTH, 29 * XHIGHT)];
    self.tuijianLabel = [[UILabel alloc] initWithFrame:CGRectMake(27 * XWIDTH, 4 * XHIGHT, 100 * XWIDTH, 21 * XHIGHT)];
    self.tuijianImageView = [[UIImageView alloc] initWithFrame:CGRectMake(9 * XWIDTH, 9 * XHIGHT, 11 * XWIDTH, 11 * XHIGHT)];
    
    self.smallTilteView = [[UIView alloc] initWithFrame:CGRectMake(10 * XWIDTH, 10 * XHIGHT, 355 * XWIDTH, 29 * XHIGHT)];
    self.atuijianImageView = [[UIImageView alloc] initWithFrame:CGRectMake(9 * XWIDTH, 9 * XHIGHT, 11 * XWIDTH, 11 * XHIGHT)];
    
    self.atuijianLabel = [[UILabel alloc] initWithFrame:CGRectMake(27 * XWIDTH, 4 * XHIGHT, 100 * XWIDTH, 21 * XHIGHT)];
}


-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.anewThemesArray.count;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    Themes *atheme = [[Themes alloc] init];
    atheme = [self.anewThemesArray objectAtIndex:section];
    NSMutableArray *rowcountArray = [self.chanellDic objectForKey:[NSString stringWithFormat:@"%ld",atheme.id]];
    return rowcountArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    self.shequCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"sehqucell" forIndexPath:indexPath];
    
    Themes *atheme = [[Themes alloc] init];
    atheme = [self.anewThemesArray objectAtIndex:indexPath.section];
    NSMutableArray *aaArray = [NSMutableArray array];
    aaArray = [self.chanellDic objectForKey:[NSString stringWithFormat:@"%ld",atheme.id]];
    
    Channels *achannel = [aaArray objectAtIndex:indexPath.row];
    self.shequCell.achannel = achannel;
    
    self.shequCell.backgroundColor = [UIColor whiteColor];
    return self.shequCell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
   
    self.communityDetailVC = [[CommunityDetailViewController alloc] init];
//    [self.myAgent hidesBottomBar];
    self.communityDetailVC.hidesBottomBarWhenPushed = YES;
   

    Themes *atheme = [[Themes alloc] init];
    atheme = [self.anewThemesArray objectAtIndex:indexPath.section];
    NSMutableArray *channelArray = [self.chanellDic objectForKey:[NSString stringWithFormat:@"%ld",atheme.id]];

    self.communityDetailVC.achannel = [channelArray objectAtIndex:indexPath.row];
    [SHAREDAPPDELE.myTB isHiddenTabBar:YES withViewController:self.communityDetailVC
     ];
     [self.navigationController pushViewController:self.communityDetailVC animated:YES];
}



//点击更多按钮触发
-(void)pushMoreVC:(NSInteger)section
{
    MoreViewController *moreVC = [[MoreViewController alloc] init];
  //  [self.myAgent hidesBottomBar];
    moreVC.hidesBottomBarWhenPushed = YES;
//    [self.navigationController pushViewController:self.communityDetailVC animated:NO];
    [self.navigationController pushViewController:moreVC animated:NO];
    
  moreVC.achannel = [self.anewThemesArray objectAtIndex:section];
    moreVC.atheme = [self.anewThemesArray objectAtIndex:section];
}

- (void)push:(NSInteger)a
{
   
    self.wheelVC.hidesBottomBarWhenPushed = YES;
    Lunbo * alunbo = [self.lunboArray objectAtIndex:a];
    self.wheelVC.alunbo = alunbo ;
    [self.navigationController pushViewController:self.wheelVC animated:NO];

    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
